﻿
var itemsPerPage = 4, ci = itemsPerPage-1, total = 0, minLeft = 0, maxLeft = 0, left = 0, speed = 15000;
var obj, s, w, st, items, noOfPages, currPage=1, noOfDisplayPages=5;

function projectSlideInit(){
	obj = jQuery('#projectList');
	items = jQuery('#projectList > .item');											
	total = s = items.length;
	w = jQuery('.item').width();		
	noOfPages =parseInt(total % itemsPerPage == 0 ? (total / itemsPerPage) : ((total / itemsPerPage)+1));
	writeProjectPagesList(noOfPages);
	if(noOfDisplayPages > noOfPages)
		noOfDisplayPages = noOfPages;
	if (total > itemsPerPage){	
		$(".pageListChild").css("display","none");
		for(var i=1;i<= noOfDisplayPages;i++)
			$("#page"+i).show();
		$("#prevButton").css("display","none");
		$("#page1").css("font-weight","bold");

		// Reset width for slider
		obj.css('width',w*s+'px');	
		jQuery(obj).css('width',w*s+'px').css('margin-left','0');
		
		// Mouse click
		jQuery('.projectPaging #nextButton').click(function(){nextButton();});
		jQuery('.projectPaging #prevButton').click(function(){prevButton();});
	}
	else{
		jQuery(obj).css('width',w*s+'px').css('margin-left','0');
		jQuery('.projectPaging').remove();
	}
}
function writeProjectPagesList(noOfPages){
	var pageList = jQuery('#pageList');
	for(var i = 1; i <= noOfPages; i++)
		pageList.append(jQuery('<span class="pageListChild" id="page' + i + '" onClick="pageListButton(this.id)"> '+ i +' </span>'));
}
function pageListButton(id){	

	$("#prevButton").show();
	$("#nextButton").show();
	var page= $("#"+id);
	$(".pageListChild").css("font-weight","normal");
	page.css("font-weight","bold");
	var pageIndex = parseInt(id.charAt(4));
	currPage=pageIndex;

	left = -((pageIndex-1) * itemsPerPage * w);
	
	jQuery(obj).animate(
		{marginLeft: left}, 
		{queue:false, duration:800}
	)	
	if(currPage==noOfPages)
	{
		$("#nextButton").css("display","none");
	}
	if(currPage == 1)
	{
		$("#prevButton").css("display","none");
	}
	adjustPageListShow();
}

function nextButton(){	
	
	if(currPage < noOfPages)
	{
		$("#prevButton").show();
		currPage ++;
		left -= itemsPerPage*w;			
	
		jQuery(obj).animate(
			{marginLeft: left}, 
			{queue:false, duration:800}
		)	
		$(".pageListChild").css("font-weight","normal");
		$("#page"+currPage).css("font-weight","bold");
		if(currPage == noOfPages)
		{
			$("#nextButton").css("display","none");
		}
		adjustPageListShow();
		
	}
}

function prevButton(){
	
	if(currPage > 1)
	{
		$("#nextButton").show();	
		currPage --;
		left += itemsPerPage*w;							

		jQuery(obj).animate(
			{marginLeft: left}, 
			{queue:false, duration:800}
		)	
		$(".pageListChild").css("font-weight","normal");
		$("#page"+currPage).css("font-weight","bold");
		if(currPage == 1)
		{
			$("#prevButton").css("display","none");
		}
		adjustPageListShow();
	}
}

function adjustPageListShow()
{
	$(".pageListChild").css("display","none");
	if(currPage <= parseInt(noOfDisplayPages/2))
	{
		for(var i=1;i<= noOfDisplayPages;i++)
			$("#page"+i).show();
	}	
	else
		if(currPage >= (noOfPages-parseInt(noOfDisplayPages/2)))
		{
			for(var i=0;i < noOfDisplayPages;i++)
				$("#page"+(noOfPages-i)).show();
		}	
		else	
		{
			if(noOfDisplayPages%2!=0)
				for(var i=currPage-parseInt(noOfDisplayPages/2);i<=currPage+parseInt(noOfDisplayPages/2);i++)
					$("#page"+i).show();
			else
				for(var i=currPage-(noOfDisplayPages/2)+1;i<=currPage+(noOfDisplayPages/2);i++)
					$("#page"+i).show();	
		}
}

function adjustNext(){											
	ci+=itemsPerPage;
	if (left==minLeft){																								
		jQuery('#projectList div.item:lt('+itemsPerPage+')').remove();
		var curInd = ci%total;
		for(i=0;i<itemsPerPage;i++){
			curInd += 1;
			if (curInd > total-1) curInd = 0;
			obj.append(items.eq(curInd).clone());
		}
		obj.css('margin-left',left+(w*itemsPerPage)+'px');													
		if(ci>total) ci=ci%total;			
	}
}

function adjustPrev(){					
	ci-=itemsPerPage;
	if(left==0){												
		jQuery('#projectList div.item:gt('+(s-itemsPerPage-1)+')').remove();
		var curInd = ci - itemsPerPage;	
		if(curInd < 0) curInd = total + curInd;
		for(var i=0;i<itemsPerPage;i++){
			if(curInd<0) curInd = total - 1;
			obj.prepend(items.eq(curInd).clone());
			curInd--;
		}
		obj.css('margin-left',maxLeft+'px');
		if(ci<0) ci = ci += total;
	}
}